package com.lanqiao.companymanage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.lanqiao.companymanage.entity.Admin;
import com.lanqiao.companymanage.entity.User;
import com.lanqiao.companymanage.enums.UserTypeEnum;
import com.lanqiao.companymanage.mapper.AdminMapper;
import com.lanqiao.companymanage.mapper.UserMapper;
import com.lanqiao.companymanage.service.UserService;
import com.lanqiao.companymanage.utils.MD5Utils;
import com.lanqiao.companymanage.utils.Result;
import io.swagger.annotations.Api;
import org.apache.commons.mail.HtmlEmail;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 登陆界面
 * @Author wucaijie
 * */
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdminMapper adminMapper;
    /**
     * 登录操作
     * @param username
     * @param password
     * @param userType
     * @param request
     * @return
     */
    @PostMapping("/a")
    @ResponseBody
    public Result login(@Param("username") String username,
            @Param("password") String password,
            @Param("userType") String userType, HttpServletRequest request){


        if(StringUtils.isEmpty(username)){
            return Result.err("请填写用户名");
        }
        if(StringUtils.isEmpty(password)){
            return Result.err("请填写密码");
        }
        UserTypeEnum userTypeEnum = UserTypeEnum.getEventByCode(userType);
        HttpSession session=request.getSession();

        Integer result=0;
        switch (userTypeEnum){
            case ADMIN:
               Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username",username)
                        .eq("password", password));
                if(admin==null){
                   return Result.err("账号或密码错误");
                }
                    session.setAttribute("loginUserType", UserTypeEnum.USER.getCode());
                    session.setAttribute("loginUserId",admin.getId());
                    result=1;
                    break;
            case USER:
                User user = userMapper.selectOne(new QueryWrapper<User>().eq("username",username)
                        .eq("password",password));
                if(user==null){
                   return Result.err("账号或密码错误");
                }
                session.setAttribute("loginUserType", UserTypeEnum.ADMIN.getCode());
                session.setAttribute("loginUserId",user.getId());
                result=2;
                break;
        }
        //登录失败
        return Result.success(result.toString());
    }

    /**
     * 邮箱验证
     * @author wucaijie
     * */
    //邮箱验证码
    @PostMapping("/a1")
    @ResponseBody
    public Result sendEmail(String emailaddress,String userType, HttpServletRequest request) {
        Random random = new Random();
        int num = random.nextInt(9999 - 1000 + 1) + 1000;
        UserTypeEnum userTypeEnum = UserTypeEnum.getEventByCode(userType);
        HttpSession session = request.getSession();

        List list1=new ArrayList();
        list1.clear();
        Integer i=0;
        switch (userTypeEnum) {
            case ADMIN:
                try {
                    HtmlEmail email = new HtmlEmail();//不用更改
                    email.setHostName("smtp.qq.com");//需要修改，126邮箱为smtp.126.com,163邮箱为163.smtp.com，QQ为smtp.qq.com
                    email.setCharset("UTF-8");
                    email.addTo(emailaddress);// 收件地址

                    email.setFrom("1148079797@qq.com", "wsnm");//此处填邮箱地址和用户名,用户名可以任意填写

                    email.setAuthentication("1148079797@qq.com", "kjhgveriasxhfhbc");//此处填写邮箱地址和客户端授权码

                    email.setSubject("wsnm");//此处填写邮件名，邮件名可任意填写
                    email.setMsg("尊敬的用户您好,您本次注册的验证码是:" + num);//此处填写邮件内容

                    email.send();
                    session.setAttribute("loginUserType", UserTypeEnum.ADMIN.getCode());
                    i=1;
                    list1.add(i);
                    list1.add(num);
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.err("登录失败");
                }
                break;
            case USER:
                try {
                    HtmlEmail email = new HtmlEmail();//不用更改
                    email.setHostName("smtp.qq.com");//需要修改，126邮箱为smtp.126.com,163邮箱为163.smtp.com，QQ为smtp.qq.com
                    email.setCharset("UTF-8");
                    email.addTo(emailaddress);// 收件地址

                    email.setFrom("1148079797@qq.com", "wsnm");//此处填邮箱地址和用户名,用户名可以任意填写

                    email.setAuthentication("1148079797@qq.com", "kjhgveriasxhfhbc");//此处填写邮箱地址和客户端授权码

                    email.setSubject("wsnm");//此处填写邮件名，邮件名可任意填写
                    email.setMsg("尊敬的用户您好,您本次注册的验证码是:" + num);//此处填写邮件内容

                    email.send();
                    session.setAttribute("loginUserType", UserTypeEnum.USER.getCode());
                    i=2;
                    list1.add(i);
                    list1.add(num);

                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.err("登录失败");
                }
                break;
        }
       return Result.success(list1);
    }
    /**判断类型传*/
    @GetMapping("/getLogin")
    public String getCurrentLoginUser() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        String loginUser = session.getAttribute("loginUserType").toString();
        return loginUser;
    }
    /**
     * 注销
     * @author wucaijie
     * */
    @RequestMapping("/logout")
    public Boolean logout() {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession session = request.getSession();
            session.removeAttribute("loginUserType");
            session.removeAttribute("loginUserId");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
